#!/usr/bin/env python
# -*- coding: UTF-8 -*-

# Magpie - an open source discussion forum system.
#
# Copyright (C) 2009 The Magpie Team
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from ajiaojr.commons.django.shortcuts import render_to_response

from ajiaojr.magpie import forms, models
from ajiaojr.magpie.views import build_template_params
from ajiaojr.magpie.views import club_writable_required
from ajiaojr.magpie.views import forum_moderator_required
from ajiaojr.magpie.views import forum_required
from ajiaojr.magpie.views import forum_writable_required
from ajiaojr.magpie.views import post_required
from ajiaojr.magpie.views import post_writable_required

from django.http import Http404
from django.http import HttpResponseRedirect
from django.utils.translation import ugettext as _

from google.appengine.api import memcache

@club_writable_required
@forum_required
@forum_writable_required
def add(request, forum_id):
  if request.method == "POST":
    f = forms.ForumPostForm(request.POST)
    if f.is_valid():
      post = f.save(commit=False)
      post.author_ip=request.META['REMOTE_ADDR']
      request.forum.add_post(post)
      return HttpResponseRedirect("/f/%d/" % request.forum.key().id())
  else:
    f = forms.ForumPostForm()
    
  p = build_template_params(_("New Post"),
                              request.get_full_path(),
                              request.user)
  p['forum'] = request.forum
  p['form'] = f
  return render_to_response(request, 'post/info.html', p)

@club_writable_required
@post_required
@forum_moderator_required
def delete(request, post_id):
  request.forum.delete_post(request.post)
  return HttpResponseRedirect("/f/%d/" % request.forum.key().id())

@club_writable_required
@post_required
@forum_writable_required
@post_writable_required
def edit(request, post_id):
  if request.method == 'POST':
    f = forms.ForumPostForm(request.POST, instance=request.post)
    if f.is_valid():
      post = f.save()
      op_record = models.OperationRecord(operator=request.user,
                                           target=post,
                                           description='EDIT')
      op_record.put()
      if post.root_post is not None:
        forward_post_id = post.root_post.key().id()
      else:
        forward_post_id = post.key().id()

      return HttpResponseRedirect('/t/%d/' % forward_post_id)
  else:
    f = forms.ForumPostForm(instance = request.post)

  p = build_template_params(_('Edit Post'),
                              request.get_full_path(),
                              request.user)
  p['form'] = f
  p['forum'] = request.forum
  return render_to_response(request, 'post/info.html', p)

@club_writable_required
@post_required
@forum_moderator_required
def lock(request, post_id):
  request.post.is_locked = not request.post.is_locked
  request.post.put()
  return HttpResponseRedirect("/t/%d/"% request.post.key().id())

@club_writable_required
@post_required
@forum_moderator_required
def pin(request, post_id):
  request.post.is_pinned = not request.post.is_pinned
  request.post.put()
  return HttpResponseRedirect("/t/%d/"% request.post.key().id())

@club_writable_required
@post_required
@forum_moderator_required
def recommend(request, post_id):
  request.post.is_recommended = not request.post.is_recommended
  request.post.put()
  return HttpResponseRedirect("/t/%d/"% request.post.key().id())

@club_writable_required
@post_required
@forum_writable_required
@post_writable_required
def reply(request, post_id):
  if request.method == 'POST':
    f = forms.ForumPostForm(request.POST)
    if f.is_valid():
      root_post = request.post.root_post
      if not root_post:
        root_post = request.post
      post = f.save(commit=False)
      post.root_post = root_post
      post.parent_post = request.post
      root_id = root_post.key().id()
      post.author_ip=request.META['REMOTE_ADDR']
      request.forum.add_post(post)
      return HttpResponseRedirect('/t/%d/' % root_id)
  else:
    f = forms.ForumPostForm()
    if not request.post.title.startswith('Re: '):
      f.fields['title'].initial = 'Re: ' + request.post.title
    else:
      f.fields['title'].initial = request.post.title
  
  p = build_template_params(_('Reply Post'),
                              request.get_full_path(),
                              request.user)
  p['form'] = f
  p['forum'] = request.forum
  return render_to_response(request, 'post/info.html', p)
